import cv2 as cv
import numpy as np

"""
load,display,save image  加载,显示,保存图像
cv.imread()
cv.imshow()
cv.imwrite() 
shape: 图像形状,长宽尺寸以及颜色通道
"""
# 默认是彩色图片,cv.imread_color
image = cv.imread("pic/01.png")
# image = cv.imread("pic/01.png",cv.IMREAD_COLOR) 和上面的是等价的

print("image width = {}".format(image.shape[1]))
print("image height = {}".format(image.shape[0]))
print("image channel = {}".format(image.shape[2]))
print(type(image.shape), image.shape)

imageHomework = cv.imread("pic/02.png")
print("image.shape = {}".format(imageHomework.shape))
h, w = imageHomework.shape[:2]
print("width = {},height = {}".format(w, h))

b, g, r = imageHomework[0, 0]
print("Pixel color at (0,0): red = {},green = {},blue = {}".format(r, g, b))
imageHomework[0, 0] = (125, 125, 125)
b, g, r = imageHomework[0, 0]
print("Pixel color at (0,0): red = {},green = {},blue = {}".format(r, g, b))

# 把这个图像分成四个等分.
centerX, centerY = w // 2, h // 2
leftTop = imageHomework[:centerY, :centerX]
rightTop = imageHomework[:centerY, centerX:]
leftBottom = imageHomework[centerY:, :centerX]
rightBottom = imageHomework[centerY:, centerX:]

cv.imshow("LeftTop", leftTop)
cv.imshow("RightTop", rightTop)
cv.imshow("LeftBottom", leftBottom)
cv.imshow("RightBottom", rightBottom)

cv.waitKey(0)

# 将左上,右上,左下,右下,改成红绿蓝白色
imageHomework[:centerY, :centerX] = (0, 0, 255)
imageHomework[:centerY, centerX:] = (0, 255, 0)
imageHomework[centerY:, :centerX] = (255, 0, 0)
imageHomework[centerY:, centerX:] = (125, 125, 125)
cv.imshow("Original", imageHomework)
cv.waitKey(0)

imageUsed = cv.imread("pic/03.png")
b, g, r = imageUsed[225, 111]
print("red = {},green = {},blue = {}".format(r, g, b))
